<?php 

namespace app\wechat\controller;

use library\Controller;
use think\Db;
use EasyWeChat\Factory;

/**
 * Class WechatTool
 * @package  app\wechat\controller\WechatTool
 */
class WechatTool extends Controller
{
	
	private static $config = [
		'app_id' => 'wxed93066ca98ccfb9',
        'secret' => '667f1a4cf236867c8ad3a36fb4ab8938',
        'key'    => '87e345eecc2fb425c3f39f1b9218a709',
        // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',
        'mch_id'     => '1330381101',
        'mch_key'    => '87e345eecc2fb425c3f39f1b9218a709',
        'log' => [
            'level' => 'debug',
            'permission' => 0777,
            'file' => RUNTIME_PATH. 'wechat/wxpay.log',
        ],
	];
    /**
     * @Author      GNLEON
     * @HttpRequest                            get|post
     * @Param                   -
     * @DateTime    2020-08-26
     * @LastTime                2020-08-26
     * @copyright   公司审核通知
     * @license     [license]
     * @return      [type]      [description]
     */
	public static function send_message($data){
		$app = Factory::officialAccount(self::$config); 

        $res = $app->template_message->send([
        'touser' => $data['openid'],
        'template_id' => 'rRYJuY3kIm19p6L22pShuUBhIKiIvEwc590GBon_auI',
        'url' => $data['url'],
        'data' =>[
             'first'=>array('value'=>$data['title'],'color'=>"#000000"),
             'keyword1'=>array('value'=>$data['name'],'color'=>"#000000"),
             'keyword2'=>array('value'=>$data['mobile'],'color'=>"#000000"),
             'keyword3'=>array('value'=>$data['time'],'color'=>"#000000"),
             'keyword4'=>array('value'=>$data['status'],'color'=>"#000000"),
             'remark'=>array('value'=>$data['remark'],'color'=>"#000000"),
        ],
    ]);
        return  tojson(['data'=>$res]);
	}
    /**
     * 获取微信模板列表
     * @HtttpRequest                          get|post
     * @Author       GNLEON
     * @Param
     * @DateTime     2020-09-11T09:11:32+0800
     * @LastTime     2020-09-11T09:11:32+0800
     * @return       [type]                   [description]
     */
    public static function getWXtpllist(){
        $app = Factory::officialAccount(self::$config); 
        return $app->template_message->getPrivateTemplates();
    }
    /**
     * 统一发送微信模板消息
     * @HtttpRequest                          get|post
     * @Author       GNLEON
     * @Param
     * @DateTime     2020-09-11T09:14:12+0800
     * @LastTime     2020-09-11T09:14:12+0800
     * @param        [type]                   $openid  [用户微信openid]
     * @param        [type]                   $data    [
     *                                                  'template_id' 微信模板id
     *                                                  'array'      模板数据
     *                                                     ]
     * @param        string                   $url     [跳转链接]
     * @return       [type]                            [description]
     */
    public static function send_WXtpl($openid,$data,$url = 'index'){
        $app = Factory::officialAccount(self::$config);
        $res = $app->template_message->send([
        'touser' => $openid,
        'template_id' => $data['template_id'],
        'url' => $url,
        'data' =>$data['array'],
    ]);
        return  tojson(['data'=>$res]);
    }
    /**
     * 群发消息
     * @HtttpRequest                              get|post
     * @Author       GNLEON
     * @Param
     * @DateTime     2020-11-04T09:59:07+0800
     * @LastTime     2020-11-04T09:59:07+0800
     * @param        [type]                       $message    [信息]
     * @param        [type]                       $openid_arr [数组 openid]
     * @return       [type]                                   [data]
     */
    public static function send_allmessage($message,$openid_arr){
        $app = Factory::officialAccount(self::$config);
        $res = $app->broadcasting->sendText($message, $openid_arr);
        return tojson(['data'=>$res]);
    }
    public static function  toshorturl($str){
        $app = Factory::officialAccount(self::$config);
        $shortUrl = $app->url->shorten($str);
        if($shortUrl){
            $shortUrl = $shortUrl['short_url'];
        }
        return $shortUrl;
    }

}